SMLFormat: Pretty Printer for Standard ML

@author YAMATODANI Kiyoshi
@version $Id: OVERVIEW_ja.txt,v 1.9 2008/08/10 13:44:01 kiyoshiy Exp $

========================================
1. SMLFormat

SMLFormat�͎��̓����\�������B

  * smlformat
  * formatlib


====================
1.1. smlformat

smlformat�͒P�ƂŎg�p����c�[���ł���B
smlformat�ւ̓��͂�type/datatype�錾���܂�SML�\�[�X�t�@�C��(*.ppg)�ł���B
����type/datatype�錾�ɂ́A���̌^�̒l�𕶎��񉻂��ďo�͂���ۂ̏������A����ȃR�����g�`���Ŏw�肷��B���̃R�����g���t�H�[�}�b�g�R�����g(format comment)�ƌĂԁB
smlformat�́Atype/datatype�錾�ƃt�H�[�}�b�g�R�����g�Ƃ���͂��A�t�H�[�}�b�^(formatter)�ƌĂԊ֐���SML�R�[�h�𐶐�����B
����^t�ɑ΂��Đ��������t�H�[�}�b�^�́A�^t�̒l���󂯎��A���̒l�𐮌`�o�͂��邽�߂̒��ԕ\����Ԃ��֐��ł���B���̒��ԕ\�����t�H�[�}�b�g��(format expression)�ƌĂԁB

  -------(Absyn.ppg)-----------------------------------------------
  structure Absyn =
  struct

    (*%
     *)
    datatype exp
      = (*%
         * @format(const * loc) {const}
         *)
        EXPCONSTANT of constant * loc
      | (*%
         * @format(cond * ifTrue * ifFalse * loc)
         *            N0{ "if" 2[ +d {cond} ]
         *             +1 "then" 2[ +d {ifTrue} ]
         *             +1 "else" 2[ +d {ifFalse} ] }
         *)
        EXPIF of exp * exp * exp * loc
      | (*%
         * @format(exp * rule rules * loc)
         * N0{ "case" 2[+d {exp}] 2[+1 "of" ]+ {rules(rule)(~2[ +1 "|"] +)} }
         * @format:rule(pat * exp) {{pat} + "=>" +1 {exp}}
         *)
        EXPCASE of exp * (pat * exp) list * loc

        :

  end
  -----------------------------------------------------------------

Absyn.ppg����͂Ƃ��ė^����ƁAsmlformat�͂��̂悤�ɓ��̓t�@�C���̓��e�Ƀt�H�[�}�b�^�̒�`��}������SML�t�@�C���𐶐�����B

  -------(Absyn.ppg.sml)-------------------------------------------
  structure Absyn =
  struct

    (*%
     *)
    datatype exp
      = 
        :

    fun format_exp x = ...

  end
  -----------------------------------------------------------------

����format_exp�͂��̌^�����B

  val format_exp : exp -> FormatExpression.expression list

smlformat�R�}���h�͎��̃R�}���h�I�v�V�������󂯎��B

  --stdout
    .ppg.sml�t�@�C���ɑւ��W���o�͂Ɍ��ʂ̃R�[�h�������o���B

  --with-line-directive
    �R�[�h�̈ʒu��SML�R���p�C���Ɏw������s�ԍ��f�B���N�e�B�u��
    ���ʂ̃R�[�h�ɑ}������B

====================
1.2. formatlib

formatlib�́ASMLFormat�X�g���N�`����񋟂���B
SMLFormat�X�g���N�`���́AprettyPrint�֐�����������B
prettyPrint�֐��́A�t�H�[�}�b�g���Əo�͌����������Ƃ��Ď󂯎��A�t�H�[�}�b�g���̃e�L�X�g�\�����A�����ɂȂ�ׂ����܂�悤�ɐ��`���ďo�͂���B
prettyPrint�֐��͂��̌^�����B

  val prettyPrint :
        PrinterParameter.printerParameter ->
          FormatExpression.expression list ->
            string

smlformat�����������t�H�[�}�b�^��SMLFormat.prettyPrint��p���āA���̂悤��Absyn.exp�^�̎�expression�̒l�𐮌`�o�͂��邱�Ƃ��ł���B

  print
    (SMLFormat.prettyPrint
     {newlineString = "\n", spaceString = " ", columns = 60}
     (Absyn.format_exp expression))

�����ł͌����Ƃ���60�����w�肵�Ă���B
���Ƃ��΂��̂悤�ɏo�͂����B

    123456789012345678901234567890123456789012345678901234567890
    ------------------------------------------------------------
    let
      val exn = getException context
      val message =
          case exn
            of SystemError => "SystemError"
             | UserError msg =>
               "User:" ^ msg ^
               (
                concatWith "\n"
                (
                 map (frameToString context) (getFrames context)
                )
               )
    in raise Error message end

�t�H�[�}�b�g�R�����g�ł̏����w���ύX���邱�Ƃɂ��A���Ƃ��Ύ��̂悤�ɏo�͌`���𒲐����邱�Ƃ��ł���B

    123456789012345678901234567890123456789012345678901234567890
    ------------------------------------------------------------
    let val exn = getException context
        val message = case exn of
                          SystemError => "SystemError"
                        | UserError msg =>
                          "User:" ^ msg ^
                            (
                             concatWith "\n"
                               (
                                map (frameToString context)
                                  (getFrames context)
                               )
                            )
    in raise Error message end


========================================
2. �t�H�[�}�b�g��


====================
2.1. �L�@

����col�ƃt�H�[�}�b�g���̗� exp1 ... expn ����͂Ƃ��āA
������ text �ɕϊ����邱�Ƃ�

  exp1 ... expn
  col=>
  text

�ƕ\�L����B
�����͏ȗ�����ꍇ������B

  exp1 ... expn
  =>
  text


====================
2.2. ������萔

��d���p���ň͂܂ꂽ������萔�͂��̂܂܏o�͂����B

  "jugemu"
  =>
  jugemu

�A�����镶����萔�͘A�����ďo�͂����B

  "jugemu" "jugemu"
  =>
  jugemujugemu

�Ԃɋ󔒂����݂����ꍇ�́A������萔" "���g�p����B

  "jugemu" " " "jugemu" " " "gokounosurikire"
  =>
  jugemu jugemu gokounosurikire

���̗�ŕ�����悤�ɁA" "�͎��Ԃ̋󔒂Ǝ��ʂ��ɂ����B


====================
2.3. �󔒎w���q

" "�̂����ɋ󔒎w���q(space indicator)'+'���g�p���邱�Ƃ��ł���B

  "jugemu" + "jugemu" + "gokounosurikire"
  =>
  jugemu jugemu gokounosurikire


====================
2.4. ���s�w���q

�����񒷂̍��v���w�茅���𒴂���ꍇ���o�͉͂��s����Ȃ��B

  "jugemu" + "jugemu" + "gokounosurikire" + "kaijarisuigyono"
  40=>
  jugemu jugemu gokounosurikire kaijarisuigyono
  ----------------------------------------
  1234567890123456789012345678901234567890

���s�w���q(newline indicator)�ɂ��A���s���ׂ��ʒu���w�肷�邱�Ƃ��ł���B
���s�w���q�ɂ́A1�ȏ�̐��l�ŗD�揇�ʂ��w�肷��B���̏������ق����D��x�������B

���s�̕K�v���Ȃ��ꍇ�A���s�w���q�͏o�͓��e�ɉe�����Ȃ��B

  "jugemu" 2 "jugemu" 1 "gokounosurikire" 2 "kaijarisuigyono"
  50=>
  jugemujugemugokounosurikirekaijarisuigyono
  --------------------------------------------------
  12345678901234567890123456789012345678901234567890

�o�͓��e�������Ɏ��܂�Ȃ��ꍇ�A�D��x�̍������s�w���q���珇�ɉ��s�����B

  "jugemu" 2 "jugemu" 1 "gokounosurikire" 2 "kaijarisuigyono"
  40=>
  jugemujugemu
  gokounosurikirekaijarisuigyono
  ----------------------------------------
  1234567890123456789012345678901234567890

�D��x2���D��x1���D�悳���̂ŁA�܂��A��Ԗڂ̉��s�w���q�ŉ��s�����B

�����D��x�������s�w���q�́A���̂����̂����ꂩ�̈ʒu�ŉ��s����K�v������ꍇ�A���̂��ׂĂ̈ʒu�ŉ��s�����B

  "jugemu" 2 "jugemu" 1 "gokounosurikire" 2 "kaijarisuigyono"
  20=>
  jugemu
  jugemu
  gokounosurikire
  kaijarisuigyono
  --------------------
  12345678901234567890

���̗�ł́A�D��x1�������s�w��q���ЂƂƁA�D��x2������̉��s�w���q���܂܂�Ă���B������20�ł���ꍇ�A�܂��D��x1�̉��s�w��q�ŉ��s����K�v������B����ɁA�D��x2�̉��s�w��q�̂����A�ŏ��̎w���q�ł͉��s����K�v���Ȃ����A�����ЂƂ̉��s�w���q�ł͉��s�̕K�v������B���̂��߁A�����̉��s�w���q�̈ʒu�ŉ��s�����B

�����ɂ́A�D��x��n�Ƃ�����s�w��qN�̈ʒu�ŉ��s���ׂ����ǂ��������̂悤�Ɍ��肷��B

  1) n�Ɠ��������邢��n���Ⴂ�D��x�̉��s�w��q�ŉ��s����ꍇ�͉��s����B

  2) �D��x��n��荂�����s�w��q�̂���N�̑O������ь���Œ��߂̓�̎w��q��F��B�Ƃ���B���̓�̐��l�k��W���r���AW < L �ƂȂ�ꍇ�͉��s����B

    L = F��B�Ƃɋ��܂ꂽ�t�H�[�}�b�g���̗�����s�����ɏo�͂��邽�߂ɕK�v�Ȍ���

    W = F�̈ʒu�ŉ��s�����Ɖ��肵���ꍇ��F�̒���̃J�����ʒu����A�w�茅���܂ł̌���

         F         N                        B
     ... 1  "abc" +3 "def" 4 "ghi" +3 "jkl" 2 ...
          <-------------------------------->

    F��B�ɂ͂��܂ꂽ�t�H�[�}�b�g�������s�Ȃ��ŏo�͂���ɂ� 14(= 3 + 1 + 3 + 0 + 3 + 1 + 3)���K�v�ł���B�܂��AF�ŉ��s����ƁA���̒���͈�J�����ڂ���o�͂��J�n�����B�܂�AF�̒���ł̃J�����ʒu����w�茅���܂ł̌����́A�w�茅���ɓ������B���������āA�w�茅����14����菭�Ȃ��ꍇ�ɂ�N�ł̉��s���K�v�ɂȂ�B��Ő�������悤�ɁAF�̒���̃J�����ʒu�͕K���������J�����ł���Ƃ͌���Ȃ��B

  3) �ȏ�̂�����ɂ��Y�����Ȃ��ꍇ�A���s���Ȃ��B

���ӁF������萔����"\n"���g�p����Ώ�ɂ��̈ʒu�ŉ��s����邪�A���݂�SMLFormat�͕�����萔���ɉ��s�������̑��^�u�����Ȃǂ̏����������܂߂邱�Ƃ�F�߂Ă��Ȃ��B


==========
2.4.1. �󔒎w���q�Ɖ��s�w���q

�󔒎w���q�Ɖ��s�w���q�͑g�ݍ��킹�Ďg�p���邱�Ƃ��ł���B���̏ꍇ�A���s�̕K�v���Ȃ��Ȃ�΋󔒈ꕶ�����o�͂����B

  "jugemu" +2 "jugemu" +1 "gokounosurikire" +2 "kaijarisuigyono"
  50=>
  jugemu jugemu gokounosurikire kaijarisuigyono
  --------------------------------------------------
  12345678901234567890123456789012345678901234567890

���s�̕K�v������ꍇ�́A�󔒂��o�͂����ɂ��̈ʒu�ŉ��s����B

  "jugemu" +2 "jugemu" +1 "gokounosurikire" +2 "kaijarisuigyono"
  40=>
  jugemu jugemu
  gokounosurikire kaijarisuigyono
  ----------------------------------------
  1234567890123456789012345678901234567890

���ӁF��L�̌��ʂ�L���ɂ���ɂ́A�󔒕����Ɖ��s�����𑱂��Ďw�肵�Ȃ���΂Ȃ�Ȃ��B�󔒎w���q�Ɖ��s�w���q�̊Ԃɋ󔒂����ނƂ��ꂼ��ʂɉ��߂���A�󔒎w���q�͂˂ɋ󔒕������o�͂���B

���ӁF������萔" "���g�p�����ꍇ�ɂ͉��s�̗L���ɂ�����炸��ɋ󔒈ꕶ�����o�͂����B

  "jugemu" +2 "jugemu" " "1 "gokounosurikire" +2 "kaijarisuigyono"
  40=>
  jugemu jugemu 
  gokounosurikire kaijarisuigyono
  ----------------------------------------
  1234567890123456789012345678901234567890

������ɂ������A�o�͌��ʂ̈�s�ڂ̍s���ɂ͋󔒈ꕶ�����o�͂���Ă���B

==========
2.4.2. �����s�w���q

����܂ł̉��s�w���q�ɂ́A���ׂėD��x���w�肵���B
�D��x���w�肵�Ȃ����s�w���q���g�p���邱�Ƃ��ł���B
���������s�w���q(deferred newline indicator)�ƌĂԁB
����܂ł̗D��x���w�肷����s�w���q��D����s�w���q(preferred newline indicator)�ƌĂԁB
�����s�w���q��'d'�ƕ\�L����B
���ׂĂ̗D����s�w���q�̈ʒu�ŉ��s���Ă��w�茅���𒴉߂���ꍇ�Ɍ���A�����s�w���q���w�肳�ꂽ�ʒu�ŉ��s�����B

  "jugemu" +d "jugemu" +1 "gokounosurikire" +2 "kaijarisuigyono"
  30=>
  jugemu jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̗�ł́A�D����s�w���q�ł͉��s����Ă��邪�A�����s�w���q�ł͉��s����Ȃ��B
����ɏ����Ȍ������w�肷��Ɨ����s�w���q�̈ʒu�ł����s�����B

  "jugemu" +d "jugemu" +1 "gokounosurikire" +2 "kaijarisuigyono"
  10=>
  jugemu
  jugemu
  gokounosurikire
  kaijarisuigyono
  ----------
  1234567890


==========
2.4.3. �����s�w���q�Ԃ̊֌W

�O�ɏq�ׂ��悤�ɁA������s�w���q�ŉ��s�̕K�v������ꍇ�A����Ɠ������A�����荂���D��x�̉��s�w���q�̂��ׂĂŉ��s�����B���������āA���̗�ł́A�D��x2�̉��s�w���q�ŉ��s����K�v���Ȃ��ɂ�������炸�A�Ō�̗����s�w���q�ŉ��s�̕K�v�����邽�߁A���ׂẲ��s�w���q�ŉ��s�����B

  "jugemu" +2 "jugemu" +1 "gokounosurikire" +d "kaijarisuigyono"
  30=>
  jugemu
  jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

�������A�����s�w���q�̊Ԃł́A�D��x�̗D��͒�`����Ȃ��B���������āA���̗�ł́A�O�Ԗڂ̗����s�w���q�ł͉��s���邪�A�ŏ��̗����s�w���q�ł͕K�v���Ȃ����߉��s���Ȃ��B

  "jugemu" +d "jugemu" +1 "gokounosurikire" +d "kaijarisuigyono"
  30=>
  jugemu jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890


==========
2.5. �C���f���g�X�^�b�N

���s�w���q�ŉ��s����ۂ̍s���C���f���g�����A�C���f���g���ƒ����'['�Ŏw�肷�邱�Ƃ��ł���B']'�ɂ��A�C���f���g���w��̓L�����Z�������B

  "jugemu" +2 "jugemu" 5[ +1 "gokounosurikire" ] +2 "kaijarisuigyono"
  40=>
  jugemu jugemu
       gokounosurikire kaijarisuigyono
  ----------------------------------------
  1234567890123456789012345678901234567890

2�Ԗڂ̉��s�w���q�̒��O�ŃC���f���g��5���w�肳��Ă��邽�߁A�o�͌��ʂ̓�s�ڂ̍s����5���C���f���g����Ă���B
3�Ԗڂ̉��s�w���q�ł́A���O�ɃC���f���g���̎w�肪��������邽�߁A���s����ۂ̃C���f���g���̓f�t�H���g��0�ƂȂ�B

  "jugemu" +2 "jugemu" 5[ +1 "gokounosurikire" ] +2 "kaijarisuigyono"
  30=>
  jugemu
  jugemu
       gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

�C���f���g���w���3�Ԗڂ̉��s�w���q�̌��Ŏ������悤�ύX����ƁA�o�͌��ʂ͂��̂悤�ɂȂ�B

  "jugemu" +2 "jugemu" 5[ +1 "gokounosurikire" +2 "kaijarisuigyono"]
  30=>
  jugemu
  jugemu
       gokounosurikire
       kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

�C���f���g���̓X�^�b�N��ɊǗ������B
'['�́A�C���f���g�X�^�b�N�̐擪�ɃC���f���g�����v�b�V�����邱�Ƃ��w�����A']'�́A�C���f���g�X�^�b�N�̐擪�v�f���|�b�v���邱�Ƃ��w������B
������s�w���q�ŉ��s����ۂ̃C���f���g���́A���̎��_�ŃC���f���g�X�^�b�N�ɐς܂�Ă���C���f���g���̍��v�l�ɂ�茈�肳���B

  "jugemu" +2 "jugemu" 5[ +1 "gokounosurikire" 3[ +2 "kaijarisuigyono"]];
  30=>
  jugemu
  jugemu
       gokounosurikire
          kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̗�ł́A�O�Ԗڂ̉��s�w���q�̈ʒu�ŉ��s����ꍇ�̃C���f���g����3 + 5 = 8���ƂȂ�B

  "jugemu" 3[ +1 "jugemu" 5[ +2 "gokounosurikire" 3[ +3 "kaijarisuigyono"]]]
  30=>
  jugemu
     jugemu
          gokounosurikire
             kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̎���3�Ԗڂ̉��s�w���q�ŉ��s����ۂ̃C���f���g���́A3 + 5 + 3 = 11���ƂȂ�B

�C���f���g���ɂ͕������w�肷�邱�Ƃ��ł���B

  "jugemu" +2 "jugemu" 5[ +1 "gokounosurikire" ~3[ +2 "kaijarisuigyono"]];
  30=>
  jugemu
  jugemu
       gokounosurikire
    kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̗�ł́A�O�Ԗڂ̉��s�w���q�ł̃C���f���g����5 + (-3) = 2���ƂȂ�B

���ӁF�C���f���g���̍��v��0��菬�����Ȃ�ꍇ�A�G���[�ƂȂ�B
���Ƃ��΁A���̃t�H�[�}�b�g���ł͎O�Ԗڂ̉��s�w���q�ŉ��s����ꍇ�ɃC���f���g���� 3 + (-5) = -2 �ƂȂ邽�߃G���[�ƂȂ�B

  "jugemu" +2 "jugemu" 3[ +1 "gokounosurikire" ~5[ +2 "kaijarisuigyono"]];

====================
2.6. �K�[�h

�ȏ�ł́A���ׂẲ��s�w���q�̗D��x���O���[�o���X�R�[�v�����L���Ă����B
�K�[�h(guard)��p����ƁA�D��x�̃X�R�[�v�𕪊�������l�X�g���邱�Ƃ��ł���B

�K�[�h�́A�t�H�[�}�b�g���̗��"{"��"}"�ň͂ނ��ƂŒ�`����B

  {"jugemu" +2 "jugemu"} +1 {"gokounosurikire" +2 "kaijarisuigyono"}

���ӁF�C���f���g�X�^�b�N�̑Ή�����v�b�V���ƃ|�b�v���قȂ�K�[�h�ɑ����Ă͂Ȃ�Ȃ��B
���Ƃ��Ύ��̃t�H�[�}�b�g���͐������Ȃ��B

  {"jugemu" 2[ +2 "jugemu"} +1 {"gokounosurikire" ] +2 "kaijarisuigyono"}


==========
2.6.1. �K�[�h�̃l�X�g

�K�[�h���l�X�g�������ꍇ�A�͂ޑ��̃K�[�h�ɑ�������s�w���q�́A�͂܂�鑤�̉��s�w���q��荂���D��x�����B

  {{"jugemu" +1 "jugemu"} +1 "gokounosurikire" +2 "kaijarisuigyono"}
  30=>
  jugemu jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

��̗�ł́A�ŏ��̉��s�w���q�͗D��x1���w�肳��Ă��邪�A�O���̃K�[�h�ɑ�������s�w���q���D��x���Ⴂ�B���̂��߁A�O���̃K�[�h�ɑ�����D��x1�����2�̉��s�w���q�ŉ��s����Ă���ɂ�������炸�A�����̃K�[�h�̉��s�w���q�ŉ��s����Ă��Ȃ��B

�O���̃K�[�h�ɑ���������s�w���q�ƁA�����̃K�[�h�ɑ�����D����s�w���q�Ƃ̊Ԃɂ́A�D��֌W���K�肵�Ă��Ȃ��B

  {"jugemu" +d "jugemu" +1 {"gokounosurikire" +1 "kaijarisuigyono"}}
  30=>
  jugemu jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

==========
2.6.2. �K�[�h�̕���

��܊֌W�ɂȂ��K�[�h�Ԃł́A�����Ɋ܂܂����s�w���q�̊ԂɗD��x�̗D��֌W�͒�`����Ȃ��B

  {"jugemu" +2 "jugemu"} +1 {"gokounosurikire" +2 "kaijarisuigyono"}
  30=>
  jugemu jugemu
  gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

�ŏ��̉��s�w���q�ƎO�Ԗڂ̉��s�w���q�͕ʂ̃K�[�h�ɑ����Ă���B
���������āA�O�Ԗڂ̉��s�w���q�ŉ��s���Ă��邪�A�ŏ��̉��s�w���q�ł͕K�v���Ȃ����߉��s���Ă��Ȃ��B


==========
2.6.3. �K�[�h�Ɗ�J����

���s�w���q�ł̉��s��̐擪�J�����́A���m�ɂ́A��J����(base column)�ƁA�K�[�h���ŃC���f���g�X�^�b�N�Ƀv�b�V�����ꂽ�C���f���g���̍��v�Ƃ����Z�����l�ɂ�茈�肳���B
�K�[�h�Ɉ͂܂�Ă��Ȃ��ꍇ�A��J������0�ł���B

�K�[�h���J�n�����ƁA�K�[�h���J�n���ꂽ�J�����ʒu���K�[�h���̊�J�����ƂȂ�B

  "jugemu" + { "jugemu" 5[ +1 "gokounosurikire" ] ~3[ +1 "kaijarisuigyono" ]}
  30=>
  jugemu jugemu
              gokounosurikire
      kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̗�ł́A�����̃K�[�h�͓�Ԗڂ�"jugemu"�̊J�n�ʒu�A���Ȃ킿8���ڂ���J�n�����B���������āA�K�[�h���̊�J������8�ƂȂ�B�K�[�h���̉��s�w���q�ŉ��s����ꍇ�̐擪�J�����́A���ꂼ��A8 + 5 = 13, 8 + (-3) = 5�ƂȂ�B

���̗�ł́A�K�[�h�̑O��ŃC���f���g�X�^�b�N���v�b�V���E�|�b�v����Ă���B

  "jugemu" 3[ +1 { "jugemu" 5[ +1 "gokounosurikire"] ~3[ +1 "kaijarisuigyono"]}]
  30=>
  jugemu
     jugemu
          gokounosurikire
  kaijarisuigyono
  ------------------------------
  123456789012345678901234567890

���̏ꍇ�A�K�[�h�͍ŏ��̉��s�w���q�ɂ����s�ƃC���f���g�̒��ォ��J�n����A�K�[�h���̊�J������4�ƂȂ�B�K�[�h���̉��s�w���q�ŉ��s����ꍇ�̐擪�J�����́A���ꂼ��4 + 5 = 9, 4 + (-3) = 1�ƂȂ�B


====================
2.7. ���s�萔

���s�萔�̓o�b�N�X���b�V����'n'�ŕ\�L����B
���s�萔�̓V���^�b�N�X�V���K�[�Ƃ����Ă悢�B
���s�萔�́A���̂�����̉��s�w���q���������D��x�������s�w���q�ƁA����ɂÂ��k�������̒���ȗ�Ƃ݂邱�Ƃ��ł���B

  "jugemu" \n "jugemu"
  20=>
  jugemu
  jugemu
  --------------------
  12345678901234567890

���̗�ł́A�w�肳�ꂽ20�����̈�s�ŏo�͂ł���ɂ�������炸�A���s����Ă���B

  "jugemu" 1 { 4[ "jugemu" +2 "gokounosurikire" \n "kaijarisuigyono" ] }
  50=>
  jugemu
  jugemu gokounosurikire
      kaijarisuigyono
  --------------------------------------------------
  12345678901234567890123456789012345678901234567890

���̗�Ɍ�����悤�ɁA���s�萔���ł������ň͂ރK�[�h����ʂɈʒu���邷�ׂẲ��s�w���q�̉ӏ��ŉ��s�����B
�܂��A���s�萔�ł̉��s�́A���̉��s�w���q�Ɠ��l�ɁA�C���f���g�X�^�b�N�ɂ��������ăC���f���g�����B


========================================
3. �����w���q

SMLFormat�́A�v���O���~���O���ꏈ���n�ł̉�͍\���؂̃e�L�X�g�\���o�͏����̎����⏕����v�ȖړI�Ƃ���B
�����̃v���O���~���O����́A�v���O�����v�f�̐e�q�֌W���\�[�X�R�[�h�̃e�L�X�g�Ő������\���ł���悤�A�\���v�f�Ԃ̐����K���ɉ����Č����K�����`���Ă���B

SMLFormat�́A�t�H�[�}�b�g���̏o�͌��ʂɃv���O���~���O����̌����K���𔽉f���邽�߂Ƀt�H�[�}�b�g���Ԃ̌����͂�\�������w���q(assoc indicator)�𓱓�����B

�����w���q�́A�K�[�h�J�n�L��("{")�̒��O�Ɍ����̕����Ƌ��x�����̂悤�ɋL�q���Ďw�肷��B

  L10{ "map" + "getContents" + "documents" }
  R5{ "first" + "::" + "second" + "::" + "others" }
  N0{ "if" + "matched" +1 "then" + "Matched" +1 "else" + "Fail" }

'L'�̓K�[�h���̗v�f�Ԃ��������ł��邱�Ƃ��w������B
���l�ɁA'R'�͉E�����ł��邱�Ƃ��w������B
'N'�͌����������l�����Ȃ����Ƃ��w������B
�������x�͐��l�Ŏw�肵�A���傫�Ȑ��l����苭���������x��\���B
�K�[�h���v�f�Ԃ̌����͂͌��������ƌ������x�̗������猈�肳���B


====================
3.1. �K�[�h�̕ی�

SMLFormat�́A�����w���q���w�肳�ꂽ�K�[�h���A�K�v�ɉ����Ċ��ʂň͂ށB


==========
3.1.1. �e�q�K�[�h�̌����͂̔�r

���Ƃ��΁A���Z���Z�̌����͂�L1�A��Z���Z�̌����͂�L2�Ƃ��A�Z�p�������̂悤�ȃt�H�[�}�b�g���ŕ\���B

  L2{ L1{ "x" "+" "y" } + "*" + L1{ "v" + "w" }}

���̃t�H�[�}�b�g���̏o�͌��ʂ́A

  x + y * v + w

�ł͂Ȃ��A

  (x + y) * (v + w)

�Ƃ��ׂ��ł���B

SMLFormat�́A�K�[�h���v�f�Ԃ̌����͂ƁA���̃K�[�h��v�f�Ƃ��Ċ܂ޏ�ʃK�[�h���̗v�f�Ԍ����͂��r���āA�K�[�h�����ʂň͂ނׂ������肷��B
�܂�A�K�[�hC�̌����͂�S�Ƃ��AC���O����͂ރK�[�hP�̌����͂�T�Ƃ���ƁAS��T�����u�ア�v�Ȃ�΁AC���̗v�f���AP����C�ɗאڂ���v�f�ƌ�������̂�h������C�����ʂň͂ޕK�v������B


==========
3.1.2. ���������ƈʒu�֌W

�K�[�h�����ʂň͂ނׂ������肷�邽�߂ɂ́A�e�q�֌W�ɂ���K�[�h�̌����͊Ԃ̋���֌W�ɉ����āA�e�K�[�h���ł̎q�K�[�h�̈ʒu���l���ɓ���Ȃ���΂�Ȃ��B

���Ƃ��΁A�֐��K�p�̌����͂�L10�Ƃ��A��d�Ƀl�X�g�����֐��K�p�������̃t�H�[�}�b�g���ŕ\���B

  L10{ L10{ "f" + "x" } + L10{ "g" + "y" }}

���̃t�H�[�}�b�g���̏o�͌��ʂ�

  f x g y

�ł͂Ȃ�

  (f x) (g y)

�Ƃ��ׂ��ł���B
����ɁA�������ł��邱�Ƃ���A�����̊֐��K�p���͂ފ��ʂ���菜����

  f x (g y)

�Ƃ��邱�Ƃ��ł���B�������A�E���̊֐��K�p���͂ފ��ʂ͏ȗ��ł��Ȃ��B


���l�ɁA�֐��^���\������^�\���q'->'�̌����͂�R1�Ƃ���ƁA�t�H�[�}�b�g��

  R1{ R1{ "t1" + "->" + "t2" } + "->" + R1{ "s1" + "->" + "s2" }}

�̏o�͌��ʂ�

  t1 -> t2 -> s1 -> s2

�ł͂Ȃ�

  (t1 -> t2) -> (s1 -> s2)

�����

  (t1 -> t2) -> s1 -> s2

�Ƃ��ׂ��ł���B


====================
3.2. �����w���q�̏���

SMLFormat�́A�ȉ��ɐ�������菇�ɂ��������āA�t�H�[�}�b�g�����̌����w���q�t���K�[�h���A�����w���q�������Ȃ��K�[�h�ƁA�K�v�Ȃ�΃K�[�h���͂ފ��ʂɕϊ�����B

���ʂɂ��͂����݂̗v�s�v�́A��ʃK�[�h����u�p���v���錋���͂ƁA�K�[�h�Ɏw�肳�ꂽ�����͂Ƃ̔�r�ɂ���Č��肷��B

==========
3.2.1. �p�����錋����

��ʃK�[�h����p�����ꂽ�����͂�S�Ƃ��A�t�H�[�}�b�g����e�Ƃ���B
e�̎q�v�f�ɑ΂��Čp�����錋���͂����̂悤�Ɍ��肷��B

  e = T{ exp1 ... expk }�̏ꍇ
    T = Ln �ł���ꍇ
         exp1 ... expk ���̃K�[�h�܂��͕�����萔�̂����ł����Ɉʒu���鎮
        �ɑ΂���Ln���p������B
         ���̑��̗v�f�ɑ΂��Ă� Nn ���p������B
    T = Rn �ł���ꍇ
         exp1 ... expk ���̃K�[�h�܂��͕�����萔�̂����ł��E�Ɉʒu���鎮
        �ɑ΂���Rn���p������B
         ���̑��̗v�f�ɑ΂��Ă� Nn ���p������B
    T = Nn �ł���ꍇ
         exp1 ... expk �̂��ꂼ��ɑ΂��� Nn ���p������B

  e = {exp1 ... expk}�̏ꍇ
    S = Ln �ł���ꍇ
         exp1 ... expk ���̃K�[�h�܂��͕�����萔�̂����ł����Ɉʒu���鎮
        �ɑ΂���Ln���p������B
         ���̑��̗v�f�ɑ΂��Ă� Nn ���p������B
    S = Rn �ł���ꍇ
         exp1 ... expk ���̃K�[�h�܂��͕�����萔�̂����ł��E�Ɉʒu���鎮
        �ɑ΂���Rn���p������B
         ���̑��̗v�f�ɑ΂��Ă� Nn ���p������B
    S = Nn �ł���ꍇ
         exp1 ... expk �̂��ꂼ��ɑ΂��� Nn ���p������B

  ���̑��̏ꍇ
    e�͎q�v�f�������Ȃ��̂Ō����͂̌p���͕s�v�B


==========
3.2.2. �����w���q��'<'�֌W

�����w���q��ł̓񍀊֌W'<'�����̂悤�ɒ�`����B

     An < Bm, if n < m (A,B are L,R or N)
     Ln < Nn
     Rn < Nn
     p < q, if p < r and r < q

�܂��A

  p <> q == ((p = Ln and q = Rn) or (p = Rn and q = Ln))

�ƒ�`����B

==========
3.2.3. �K�[�h�̕ی�

��ʃK�[�h����p�����ꂽ�����͂�S�A���ʃK�[�h�Ɏw�肳�ꂽ�����w���q�̌����͂�T�Ƃ���ƁA���ʃK�[�h���͂ފ��ʂ̕K�v�������̂悤�ɔ��f����B

  a) S < T �܂��� S = T �Ȃ�΁A�K�[�h���͂ފ��ʂ͕s�v�B
  b) ���̑��A���Ȃ킿 T < S �܂��� S <> T �ł���ꍇ�A�K�[�h�����ʂň͂ށB


==========
3.2.4. �ϊ���

��ʃK�[�h����p�����������͂�S�Ƃ��A�����w���q���t�H�[�}�b�g��e�����菜���ĐV���Ɏ�e'�𓾂邱�Ƃ�

  S | e => e'

�ƕ\�L����B

��ɂ������O�̃t�H�[�}�b�g���̕ϊ��菇�������B
�p�����錋���͂̏����l�Ƃ���N0��p����B

=====
��P
 L2{ L1{ "x" "+" "y" } + "*" + L1{ "v" + "w" } }

  L1 < L2                      L1 < N2
  --------------------------   --------------------------
  L2| L1{"x" "+" "y"}          N2| L1{"v" "+" "w"}
    | => "(" {"x" + "y"} ")"     | => "(" {"v" + "w"} ")"   N0 < L2
  -----------------------------------------------------------------
  N0| L2{L1{"x" "+" "y"} + "*" + L1{"v" + "w"}}
    |        => {"(" {"x" + "y"} ")" + "*" + "(" {"v" + "w"} ")"}

=====
��Q
 L10{ L10{ "f" + "x" } + L10{ "g" + "y" } }

  L10 = L10             L10 < N10
  -------------------   ---------------------------
  L10| L10{"f" + "x"}   N10| L10{"g" + "y"}
     | => {"f" + "x"}      | => "(" {"g" + "y"} ")"   N0 < L10
  ------------------------------------------------------------
  N0| L10{L10{"f" + "x"} + L10{"g" + "y"}}
    |            => {{"f" + "x"} + "(" {"g" + "y"} ")"}

=====
��R
 R1{ R1{ "t1" + "->" + t2" } + "->" + R1{ "s1" + "->" + "s2" } }

  R1 < N1                              R1 = R1
  -------------------------------      ---------------------------
  N1| R1{"t1" + "->" + t2"}            R1| R1{"s1" + "->" + "s2"}
    | => "(" {"t1" + "->" + t2"} ")"     | => {"s1" + "->" + "s2"}  N0 < R1
  -------------------------------------------------------------------------
  N0| R1{R1{"t1" + "->" + t2"} + "->" + R1{"s1" + "->" + "s2"}}
    |       => {"(" {"t1" + "->" + t2"} ")" + "->" + {"s1" + "->" + "s2"}}

==========
3.2.5. cut

SML��let����tuple���A���R�[�h���̂悤�ɑO����L�[���[�h�����L���ň͂܂�Ă��鎮�́A���̎����u����Ă��镶���ɂ�����炸�A�J�b�R�ň͂ޕK�v���Ȃ��B
�������A�O�q�̌����w���q�ł͂��̂悤�ȏꍇ�ɓK�؂Ȍ����x���w�肷�邱�Ƃ��ł��Ȃ��B

��Ƃ��āA����SML�̎����t�H�[�}�b�g���ł���킷���Ƃ��l����B

  f (g x, y)     --(*)

SML�ł͊֐��K�p���͑��̎���荂�������x�����B
���������āAexp1, exp2�����ꂼ��֐����ƈ�������\���t�H�[�}�b�g���Ƃ���ƁA�֐��K�p���͎��̂悤�ȃt�H�[�}�b�g���ŕ\�����Ƃ��ł���B

  L10{ exp1 + exp2 }

���ɁAtuple����

  N0{ "(" exp1 "," + ... "," + expn ")" }

�ł���킷���Ƃɂ���B
����ƁA��(*)������킷�t�H�[�}�b�g����

  L10{ "f" + N0{ "(" L10{ "g" + "x" } "," + "y"")" } }

�ƂȂ�B���̃t�H�[�}�b�g�����o�͂���ƁA

                    N0 < L10
                    ---------------------------------
                    N0| L10{ "g" + "x" } => "g" + "x"
                    ------------------------------------------
                    N0| "(" L10{ "g" + "x" } "," + "y" ")"
                      |       => "(" "g" + "x" "," + "y" ")"    N0 < N10
                    ----------------------------------------------------
                    N10| N0{ "(" L10{ "g" + "x" } "," + "y"")" }
  L10| "f" => "f"      |     =>  "(" "(" "g" + "x" "," + "y" ")" ")"
  ------------------------------------------------------------------
  N0| L10{ "f" + N0{ "(" L10{ "g" + "x" } "," + "y"")" } }
    |    => "f" + "(" "(" "g" + "x" "," + "y" ")" ")"

���̂悤��tuple�����璷�ȃJ�b�R�ň͂܂�Ă��܂��B

  f ((g x, y))

�����tuple���̌����x���֐��K�p���̌����x���ア���߂ł���B
�����ŁAtuple���̍\���v�f�̌����x��N10�ɏグ��

  N10{ "(" exp1 "," + ... "," + expn ")" }

�Ƃ���B��(*)������킷�t�H�[�}�b�g����

  L10{ "f" + N10{ "(" L10{ "g" + "x" } "," + "y"")" } }

�ƂȂ�A������o�͂���ƁA

                    L10 < N10
                    ------------------------------------------
                    N10| L10{ "g" + "x" } => "(" "g" + "x" ")"
                    ------------------------------------------
                    N10| "(" L10{ "g" + "x" } "," + "y" ")"
                       |   => "(" "(" "g" + "x" ")" "," + "y" ")"
                    ---------------------------------------------
                    N10| N10{ "(" L10{ "g" + "x" } "," + "y"")" }
  L10| "f" => "f"      |  =>  "(" "(" "g" + "x" ")" "," + "y" ")"
  ---------------------------------------------------------------
  N0| L10{ "f" + N10{ "(" L10{ "g" + "x" } "," + "y"")" } }
    |    => "f" + "(" "(" "g" + "x" ")" "," + "y" ")"

�ɂ��Atuple���̊֐��K�p�����J�b�R�ň͂܂�Ă��܂��B

  f ((g x), y)

���̖��́A�K�[�h�Ɏw�肳�ꂽ�����͂��A��ʃK�[�h����p�����錋���͂Ɖ��ʃK�[�h�Ɏw�肳�ꂽ�����̗͂��҂Ɣ�r����邱�ƂɌ���������B
������������邽�߁A�����w���q���g�����A��ʃK�[�h����̌����͂̌p�����Ւf(cut)���A�K�[�h�̌����͂����ʃK�[�h�̌����͂Ƃ̂ݔ�r���邱�Ƃ��w���ł���悤�ɂ���B
�擪��"!"�Ƃ��錋���w���q��cut�t�������w���q�ƌĂсA���̎w���q���w�肳�ꂽ�K�[�h�͏�ʃK�[�h�̌����͂ɂ�����炸�A���ʂň͂܂�Ȃ����̂Ƃ���B

�����p���āAtuple��������킷�t�H�[�}�b�g����

  !N0{ "(" exp1 "," + ... "," + expn ")" }

�Ƃ���B

��(*)������킷�t�H�[�}�b�g����

  L10{ "f" + !N0{ "(" L10{ "g" + "x" } "," + "y"")" } }

�ƂȂ�A������o�͂������ʂ́A

                    N0 < L10
                    ---------------------------------
                    N0| L10{ "g" + "x" } => "g" + "x"
                    ------------------------------------------
                    N0| "(" L10{ "g" + "x" } "," + "y" ")"
                      |         => "(" "g" + "x" "," + "y" ")"
                    ---------------------------------------------
                    N10| !N0{ "(" L10{ "g" + "x" } "," + "y"")" }
  L10| "f" => "f"      |     =>  "(" "g" + "x" "," + "y" ")"
  ---------------------------------------------------------------
  N0| L10{ "f" + !N0{ "(" L10{ "g" + "x" } "," + "y"")" } }
    |    => "f" + "(" "g" + "x" "," + "y" ")"

�ɂ��A

  f (g x, y)

�ƂȂ�A�s�K�v�Ȋ��ʂ��}������Ȃ��B


========================================
4. �t�H�[�}�b�^����

SMLFormat�́Atype�錾�����datatype�錾�ɗ^����ꂽ�R�����g����t�H�[�}�b�^�֐��R�[�h�𐶐�����B


====================
4.1. ��{�t�H�[�}�b�^�֐�

int��string�ȂǑg�ݍ��݂��邢��SML Basis Library�Œ񋟂�����{�I�Ȍ^�ɑ΂��ẮA���炩����BasicFormatters�X�g���N�`���Ƀt�H�[�}�b�^�֐����p�ӂ���Ă���B
���Ƃ���int�^�ɑ΂��Ă�format_int�Astring�^�ɑ΂��Ă�format_string���p�ӂ���Ă���B

====================
4.2. type�錾�ɑ΂���t�H�[�}�b�g�R�����g

���̂悤��type�錾

  type number = int

��number�^���`���A���̌^�̃t�H�[�}�b�^���`�������B

SMLFormat�Ƀt�H�[�}�b�^�𐶐������邽�߂ɂ́A���̂悤�ɃR�����g�Œ��߂�^����B

  (*%
   *)
  type number =
                 (*%
                  * @format(value) "123"
                  *)
                 int

SMLFormat�ɑ΂���w���́A"(*%"��"*)"�ň͂܂ꂽ�R�����g�ɋL�q����B
������t�H�[�}�b�g�R�����g�ƌĂԁB
type�錾�ɑ΂���t�H�[�}�b�g�R�����g���^�錾�w�b�_�R�����g�A'='�̉E�ӂ̌^���ɑ΂���t�H�[�}�b�g�R�����g���`�^���R�����g�ƌĂԁB
�����̃t�H�[�}�b�g�R�����g�́Atype�L�[���[�h����ь^���i���̏ꍇ��"int"�j�̒��O�ɔz�u���Ȃ���΂Ȃ�Ȃ��B


==========
4.2.1. �^�錾�w�b�_�R�����g

type�錾�ɑ΂���t�H�[�}�b�^�𐶐������邽�߂ɂ͌^�錾�w�b�_�R�����g���K�v�ł���B
���̗�ł͌^�錾�w�b�_�R�����g�ɉ����L�q����K�v�͂Ȃ��̂ŋ�̃R�����g��^����B


==========
4.2.2. ��`�^���R�����g

��`�^���w�b�_�R�����g�ɂ́A�t�H�[�}�b�g�^�O���ЂƂL�q����B
�t�H�[�}�b�g�^�O�͈ȉ��̌`�����Ƃ�B

  "@format(" typepat ")" template ... template

typepat�ɂ́A�^���ɑ΂���p�^�[�����L�q����B������^�C�v�p�^�[���ƌĂԁB

template�ɂ́A�t�H�[�}�b�g�e���v���[�g���L�q����B
�t�H�[�}�b�g�e���v���[�g�́A�R�����g�Ώۂ̌^�̒l���e�L�X�g�o�͂���ۂ̏������w�肷��B
�t�H�[�}�b�g�e���v���[�g�̍\���K���̓t�H�[�}�b�g���̍\���K�����܂���̂ŁA�t�H�[�}�b�g�������̂܂܃t�H�[�}�b�g�e���v���[�g�Ƃ��ċL�q�ł���B

==========
4.2.3. �t�H�[�}�b�^�̐���

number�^�ɑ΂���SMLFormat�͂��̂悤�ȃt�H�[�}�b�^�R�[�h�𐶐�����B
�����̂��߁A"<<"��">>"�ň͂܂ꂽ�����ɂ̓t�H�[�}�b�g����SML�������݂��ċL�q���Ă���BSMLFormat������������ۂ̃R�[�h�ł́A���̕����� FormatExpression.expression list �𐶐�����B

  fun format_number x = case x of value => << "123" >>

====================
4.3. datatype�錾�ɑ΂���t�H�[�}�b�g�R�����g

datatype�錾�ɑ΂���t�H�[�}�b�g�R�����g�́A�^�錾�w�b�_�R�����g�ƁA�e�l�\���q�ɑ΂����`�^���R�����g����\�������B

  (*% *)
  datatype maybeNumber =
                        (*% @format(value) "456" *)
                        SomeNumber of number
                      | (*% @format "none" *)
                        NotNumber

��`�^���R�����g�̃t�H�[�}�b�g�^�O�ɂ́A�l�\���q�̂Ƃ�����̌^���ɑ΂���^�C�v�p�^�[���ƁA�e���v���[�g���L�q����B
�������Ƃ�Ȃ��l�\���q�ɑ΂���t�H�[�}�b�g�^�O�ł́A�^�C�v�p�^�[���͕s�v�ł���B

SMLFormat�́AmaybeNumber�ɑ΂��Ă��̃t�H�[�}�b�^�𐶐�����B

  fun format_maybeNumber x =
      case x of
          SomeNumber value => << "456" >>
        | NotNumber => << "none" >>


====================
4.4. �^�C�v�p�^�[��

SMLFormat�́A�t�H�[�}�b�g�^�O���̃^�C�v�p�^�[������A��`�^���̒l�Ƃ̃p�^�[���}�b�`�������Ȃ��R�[�h�𐶐�����B
�܂��A�^���ƃ^�C�v�p�^�[���Ƃ̏ƍ��ɂ��A�^�C�v�p�^�[�����̎��ʎq�ɑΉ�����^������ł���B

�^�C�v�p�^�[�����Ɍ���鎯�ʎq�́A�t�H�[�}�b�g�e���v���[�g���Ŏg�p���邱�Ƃ��ł���B
�t�H�[�}�b�g�e���v���[�g���̎��ʎq�́A���s���ɂ��̎��ʎq�ɑ��������l�̃t�H�[�}�b�g���\���𐶐�����R�[�h�ɕϊ������B
�܂�A�e���v���[�g���Ɍ���鎯�ʎq�́A����ɑΉ�����^�ɑ΂��Ē�`����Ă���e���v���[�g�����̈ʒu�ɓW�J���邱�Ƃɑ�������B���̂��߁A�e���v���[�g���̎��ʎq�̏o�����A�e���v���[�g�C���X�^���V�G�[�V����(template instantiation)�ƌĂԁB

�e���v���[�g�C���X�^���V�G�[�V�������g�p���āAnumber�^�ɑ΂���t�H�[�}�b�g�R�����g�����̂悤�ɕύX����B

  (*% *)
  type number =
                 (*% @format(value) value *)
                 int

SMLFormat�͂��̃t�H�[�}�b�^�𐶐�����B

  fun format_number x = case x of value => << format_int(value) >>

�^���ƃ^�C�v�p�^�[���̏ƍ��ɂ�莯�ʎqvalue�ɂ͌^��int���Ή�����̂ŁA�e���v���[�g���ɏo�����鎯�ʎqvalue�́Aint�^�̃t�H�[�}�b�^(format_int)���Ăяo���R�[�h�ɕϊ������B

maybeNumber�����l�ɕύX����B

  (*% *)
  datatype maybeNumber =
                        (*% @format(value) value *)
                        SomeNumber of number
                      | (*% @format "none" *)
                        NotNumber

  fun format_maybeNumber x =
      case x of
          SomeNumber value => << format_number(value) >>
        | NotNumber => << "none" >>


==========
4.4.1. ���ʎq�^���p�^�[��

���ʎq�͂�����̌^���Ƃ��}�b�`����^�C�v�p�^�[���Ƃ��Ďg�p�ł���B


==========
4.4.2. tuple�^���p�^�[��

tuple�^���ɑ΂��ẮA���̂悤�Ƀ^�C�v�p�^�[�����w�肷�邱�Ƃ��ł���B

  (*% *)
  type region =
               (*% @format(left * right) "left=" left + "right=" right *)
               int * int

SMLFormat�́Aregion�^�ɑ΂��Ă��̃t�H�[�}�b�^�𐶐�����B

  fun format_region x =
      case x of
          (left, right) =>
              << "left=" format_int(left) + "right=" format_int(right) >>


==========
4.4.3. ���R�[�h�^���p�^�[��

���R�[�h�^���ɂ��Ă����l�ł���B

  (*% *)
  type range =
               (*% @format({min : minimum, max}) minimum + "<->" + max *)
               {min : int, max : int}

SMLFormat�́Arange�^�ɑ΂��Ă��̃t�H�[�}�b�^�𐶐�����B

  fun format_range x =
      case x of
          {min = minimum, max} =>
              << format_int(minimum) + "<->" + format_int(max) >>


==========
4.4.4. �������Ƃ�^�\���q

SMLFormat�́Alist�̂悤�Ɉ������Ƃ�^�\���q�ɑ΂��Ă��t�H�[�}�b�^�𐶐��ł���B

  (*% *)
  datatype 'a maybe =
                     (*% @format(value) value *)
                     Something of 'a
                   | (*% @format "none" *)
                     Nothing

����maybe�ɑ΂���SMLFormat����������t�H�[�}�b�^�́A�^�ϐ�'a�ɑ΂���t�H�[�}�b�^�������ɂƂ�B������t�H�[�}�b�^�����ƌĂԁB

  fun format_maybe format_'a x =
      case x of
          Something value => << format_'a(value) >>
        | Nothing => << "none" >>


����maybe��K�p�����^���ɑ΂���t�H�[�}�b�g�R�����g�͂��̂悤�ɋL�q����B

  (*% *)
  type maybeString =
                (*% @format(str tycon) tycon(str) *)
                string maybe

�^�\���q�K�p�ɑ΂���^�C�v�p�^�[���́A
  typepat ID
�Ƃ����`���Ƃ�B
���̗�ł́A���ʎqtycon�ɂ͌^�\���qmaybe���}�b�`���A�p�^�[��str�ɂ͌^��string���}�b�`����B

�e���v���[�g���ł́A�^�\���q�Ƀ}�b�`���鎯�ʎq�ւ̈����ɁA�^�\���q�ւ̈����Ƀ}�b�`���鎯�ʎq��^����`���ŋL�q����B
���̗�ł́Atycon�ւ̈����Ƃ���str��^����`���ƂȂ�B

�⑫�F��`�^�����݂�΁A���ʎqtycon�Ƀ}�b�`����maybe�ɂ�string�������Ƃ��ė^�����Ă��邱�Ƃ͖��炩�ł���B���������āA�e���v���[�g����
    tycon(str)
�̂悤��tycon�ɑ΂������str���w�肷��̂͏璷�ł���悤�Ɏv����B
�������A����́A���ɐ�������ǉ������𓱓��������ʁA�ǉ��������ǂ̎��ʎq�ɗ^��������̂��Ή��𖾂炩�ɂ���K�v�����������߂̐���ł���B

maybeString�ɑ΂���t�H�[�}�b�^�́A���̂悤��format_maybe�̑������Ƃ���format_string��^����B

  fun format_maybeString x =
      case x of tycon => << (format_maybe format_string tycon) >>


==========
4.4.5. ���C���h�J�[�h�^���p�^�[��

 '_'�����C���h�J�[�h�Ƃ��Ďg�����Ƃ��ł���B

  (*% *)
  type wildCard =
                  (*% @format(_ * n) n *)
                  (bool * int) 

���C���h�J�[�h�^���p�^�[���́A�^�[���ɑ΂��郏�C���h�J�[�h�p�^�[���ɕϊ������B

  fun format_wildCard  x = 
      case x of
          (_, n) => 
          << format_int(n) >>


==========
4.4.6. ���p���t�����ʎq�A���x��

���ʎq�⃉�x����'d'���邢�͐����ł���ꍇ�A���s�w���q�Ƃ��ĉ��߂���邱�Ƃ�h�����߁A��d���p���ň͂܂Ȃ���΂Ȃ�Ȃ��B

  (*% *)
  type quotedFields =
                    (*% @format({'d' : fd, '1' : f1}) fd f1 *)
                    {d : int, 1 : bool}

smlformat�͎��̃t�H�[�}�b�^�𐶐�����B

  fun format_quotedFields  x = 
      case x of
           {d = fd, 1 = f1} => 
               << format_int(fd) format_bool(f1) >>


==========
4.4.7. �ǉ�����

�t�H�[�}�b�^�́A�t�H�[�}�b�g�Ώۂ̒l��t�H�[�}�b�^�����̂ق��ɒǉ��������Ƃ邱�Ƃ��ł���B

  (*%
   * @params (label)
   *)
  type 'a anyLabeled =
                      (*% @format(value) label ":" value *)
                      'a

�ǉ������̖��O�́A�^�錾�w�b�_�R�����g��"@params"�^�O�Ŏw�肷��B
�ǉ������������K�v�ȏꍇ�A

  (*%
   * @params (p1, ..., pk)
   *)

���邢��

  (*%
   * @params (p1)
   *     :
   * @params (pk)
   *)

�̂悤�ɋL�q����B

�ǉ������̓e���v���[�g���Ŏg�p�ł���B
�e���v���[�g���ɏo������ǉ��������́A���s���ɂ́A�t�H�[�}�b�^�ɑ΂��ČĂяo��������n�����t�H�[�}�b�g�e���v���[�g�ɒu����������B

anyLabeled�ɑ΂��āASMLFormat�͂��̃t�H�[�}�b�^�𐶐�����B

  fun format_anyLabeled (format_'a, label) x =
      case x of value => << label ":" format_'a(value) >>


�^�\���qT�̃t�H�[�}�b�^���ǉ��������Ƃ�ꍇ�AT�Ƀ}�b�`���鎯�ʎq���e���v���[�g���Ŏg�p����ۂɂ́A�t�H�[�}�b�^�����Ƃ͕ʂɁA�ǉ��������w�肵�Ȃ���΂Ȃ�Ȃ��B
�C�ӂ̃e���v���[�g��ǉ������œn�����Ƃ��ł���B
�t�H�[�}�b�^��j�̃t�H�[�}�b�^������k�̒ǉ��������Ƃ�ꍇ�A���̍\�����g�p����B

  ID(inst1, ..., instj)(temp1, ..., tempk)

�t�H�[�}�b�^���ǉ��������Ƃ�Ȃ��ꍇ�A���̊��ʂ͕s�v�ł���B

  ID(inst1, ..., instj)

�t�H�[�}�b�^���t�H�[�}�b�^�������Ƃ炸�A�ǉ��������Ƃ�ꍇ�́A

  ID()(temp1, ..., tempk)

�Ƃ��Ȃ���΂Ȃ�Ȃ��B

���ӁFSMLFormat�͌Ăяo���t�H�[�}�b�^�̌^�𔻒�ł��Ȃ��̂ŁA�v���O���}���Ăяo�����̃R�[�h��Ńt�H�[�}�b�^�����ƒǉ�������ʂ̊��ʂɕ����邱�Ƃɂ��A��������ʂł���悤�ɂ���B

���Ƃ���anyLabeled���g�p���鑤�͂��̂悤�ɒǉ�������n���B

  (*% *)
  type intLabeled =
                      (*% @format(num tycon) tycon(num)("INT") *)
                      int anyLabeled

���̂悤�ȃt�H�[�}�b�^�����������B

  fun format_intLabeled x =
      case x of tycon => << format_anyLabeled (format_int, "INT") tycon >>

���ӁFSMLFormat�̓t�H�[�}�b�^���v������ǉ��������e���v���[�g���Ŏw�肳��Ă��Ȃ��Ă��G���[�Ƃ��Ȃ��BSMLFormat�����������R�[�h���R���p�C������ۂɃG���[�ƂȂ�͂��ł���B


====================
4.5. �`���I��`

�����ŁA����܂łɐ��������t�H�[�}�b�^�����̌`���I�Ȓ�`�������B


==========
4.5.1. ��

�t�H�[�}�b�^�� F ���^�\���q������t�H�[�}�b�^�֐����ւ̃}�b�v�Ƃ���B�i'int'��'string'�Ȃǂ͈����[���̌^�\���q�Ƃ݂Ȃ��B�j

�^�� T �����ʎq����^�ւ̃}�b�v�Ƃ���B

�ǉ������W�� P ��ǉ��������̏W���Ƃ���B


==========
4.5.2. �t�H�[�}�b�g�e���v���[�g�ϊ�

F,T,P�̂��ƂŃt�H�[�}�b�g�e���v���[�gtemp ����t�H�[�}�b�g����SML���̍���exp �𓾂邱�Ƃ����̂悤�ɏ����B

  F,T,P | temp ==> exp

�e�e���v���[�g�ɑ΂���K�����ȉ��Ɏ����B

(STRING)
    F,T,P | "string" ==> "string"
  
(INDICATOR)
    F,T,P | sp ==> sp

(INDSTART)
    F,T,P | ind[ ==> ind[

(INDEND)
    F,T,P | ] ==> ]

(SEQ)
    F,T,P | templ1 ==> exp1    F,T,P | templ2 ==> exp2
    --------------------------------------------------
    F,T,P | templ1 templ2 ==> exp1 exp2

(GUARD)
    F,T,P | temp ==> exp
    ---------------------------
    F,T,P | { temp } ==> { exp }

(INST1)
    T(ID) = (t1,...,tj) t    T(ID1) = t1   ...  T(IDj) = tj
    F(t) = f    F(t1) = f1  ...  F(tj) = fj
    F,T,P | temp1 ==> exp1
          :
    F,T,P | tempk ==> expk
    -------------------------------------------------------
    F,| ID(ID1,...,IDj)(temp1,..., tempk)
    T,|        ==>
    P |         f(f1, ..., fj, exp1, ... , expk)(ID)

    �i���̋K���Ő��������̂�SML�̊֐��K�p���ł���B�j

(INST2)
    T(ID) = t         F(t) = f
    --------------------------
    F,T,P | ID    ==>   f(ID)

    �i���̋K���Ő��������̂�SML�̊֐��K�p���ł���B�j

(INST3)
    P = P'+{ID}
    ---------------------
    F,T,P | ID   ==>   ID

    �i���̋K���Ő��������̂�SML�̕ϐ����ł���B�j


==========
4.5.3. �t�H�[�}�b�^�{�̂̐���

���ɁAF,P�̂��ƂŃt�H�[�}�b�g�^�O�ƕϐ��� x����ь^�� te����ASML��e�𓾂�ϊ���

  F,P | @format(typepat) template, x, te  ==> e

�Ə����B
���̋K���̈Ӗ��́Ax�̓t�H�[�}�b�g�Ώۂ̒l��ێ�����ϐ��ł���Ax�̌^��te�A�t�H�[�}�b�g�^�O��te�ɑ΂��鏑�����w��������̂ł���B

te��tuple�^���܂��͌^�\���q�K�p�^���ł���ꍇ�ɂ��Ă̋K���������B
���R�[�h�^���ɂ��Ă͏ȗ�����B
'int'��'string'�Ȃǂ̊�{�^�͈����[���̌^�\���q�K�p�^���Ƃ��Ĉ����B

(TUPLEtype)
    dom(F) includes {t1,...,tj}
    T = {ID1:t1, ..., IDj:tj}
    F,T,P | temp => e
    -------------------------------------------------
    F,| @format(ID1 * ... * IDj) temp,
    P | x, (t1 * ... * tj)
      |              ==> case x of (ID1,...,IDj) => e

(TYCONAPPtype)
    dom(F) includes {t,t1,...,tj}
    T = {ID:(t1,...,tj)t, ID1:t1, ..., IDj:tj}
    F,T,P | temp => e
    --------------------------------------------------
    F,| @format((ID1, ..., IDj) ID) temp,
    P | x, (t1, ..., tj) t 
      |                          ==> case x of ID => e


==========
4.5.4. �t�H�[�}�b�^�̐���

�ȏ��p���āAF�̂��ƂŁASML��type�錾�����datatype�錾����A�t�H�[�}�b�^�֐��̃R�[�h�ƐV���ȃt�H�[�}�b�^��F'�𓾂�菇�����̋K���Ŏ����B

�����ł́A�ЂƂ̌^�\���q������type�錾�����datatype�錾�Œ�`�������̂Ƃ���B
�i'and'�ɂ���ĕ����̌^�����A�����Ē�`�����ꍇ�ɑΉ�����̂͗e�Ղł���B�j


(TYPEdec)
    x,f1,...,fj are fresh variables
    F' = F+{t:format_t}
    F'' = F'+{'a1:f1, ..., 'aj:fj}
    P = {b1,...,bk}
    F'',P | formattag,x,t  ==>  e
    -------------------------------------------------------
    F | (*% @formatparams (b1,...,bk) *)
      | type ('a1,...,'aj) t =
      |         (*% formattag *) t
      |    ==>
      |            fun format_t(f1,...,fj,b1,...,bk) x = e,
      |            F'

(DATATYPEdec)
    x,x1,...,xj,f1,...,fj are fresh variables
    F' = F+{t:format_t}
    F'' = F'+{'a1:f1, ..., 'aj:fj}
    P = {b1, ..., bk}
    F'',P | formattag1,x1,t1  ==> e1
             :
    F'',P | formattagj,xj,tj  ==> ej
    -----------------------------------------------------
    F | (*% @formatparams (b1,...,bk) *)
      | datatype ('a1,...,'aj) t =
      |           (*% formattag1 *)  D1 of t1
      |         | ...
      |         | (*% formattagj *)  Dj of tj 
      |    ==>
      |      fun format_t(f1,...,fj,b1,...,bk) x =
      |          case x of D1 x1 => e1 | ... | Dj xj => ej,
      |      F'

�l�\���q���������Ƃ�Ȃ��ꍇ�Ɋւ��Ă͏ȗ�����B


====================
4.6. ���G�ȃp�^�[��

����܂łɎ�������ł́A�^�\���q�ւ̈����Atuple�^���⃌�R�[�h�^���̗v�f�̌^�͂�������ЂƂ̎��ʎq�݂̂��琬��P���Ȃ��̂ł������B
SMLFormat�́A��蕡�G�Ȍ^�����������Ƃ��ł���B


==========
4.6.1. �l�X�g�����^���ɑ΂���p�^�[��

�܂��A�l�X�g�����^�\���q�K�p�^���A�܂�^�\���q�̈���������Ɍ^�\���q�K�p���ł���^���ɑ΂���t�H�[�}�b�^����`�ł���B

  (*% *)
  type maybeLabeledInt =
                        (*%
                         * @format(num may any) any(may(num))("INT")
                         *)
                        int maybe anyLabeled

maybeLabeledInt�̃t�H�[�}�b�^�͂��̂Ƃ���B

  fun format_maybeLabledInt x =
      case x of
         any =>
           << format_anyLabeled (format_maybe format_int, "INT") any >>


tuple�^������у��R�[�h�^���̗v�f���Atuple�^�����邢�̓��R�[�h�^���A�^�\���q�K�p�^���ł���ꍇ���t�H�[�}�b�^���`�\�ł���B
����maybeRange�́A���R�[�h�^���̗v�f�Ɍ^�\���q�K�p�^����������ł���B

  (*% *)
  type maybeRange =
       (*%
        * @format({min : min minMaybe, max : max maxMaybe})
        *       minMaybe(min) "<->" maxMaybe(max)
        *)
       {min : int maybe, max : int maybe}

  fun format_maybeRange x =
      case x of
        {min = minMaybe, max = maxMaybe} =>
         <<
          (format_maybe format_int minMaybe) "<->"
          (format_maybe format_int maxMaybe)
         >>


==========
4.6.2. �����^���Ǝ��ʎq�Ƃ̃}�b�`

����܂ł̗�ł́A�^�C�v�p�^�[�����̎��ʎq�́A�ƍ��Ώۂ̌^������int��string�Ȃǂ̒P���^��maybe�Ȃǂ̌^�\���q���Ƃ̂݃}�b�`���Ă����B
�t�H�[�}�b�g�e���v���[�g�̍\���ł́A���ʎq�Ƀ��R�[�h�^����^�\���q�K�p�^���ȂǕ����^�����}�b�`�����ꍇ�ɁA���̃t�H�[�}�b�g���@���L�q���邱�Ƃ��ł��Ȃ��B

��Ƃ��āA����type�錾�ɑ΂��ăt�H�[�}�b�^���`���邱�Ƃ��l����B

  (*% *)
  type labeledRange =
       (*%
        * @format(range any) any(range) <== ???
        *)
       {min : int, max : int} anyLabeled

���ʎqrange�Ƀ��R�[�h�^�����}�b�`���邪�A���̃��R�[�h�^�̒l�ɑ΂���t�H�[�}�b�g���@���L�q���A�����anyLabeled�̃t�H�[�}�b�^�ɓn����i���Ȃ��B

���̐�������������@�Ƃ��ẮA���̂悤�ɕ�����type/datatype�錾�ɕ������邱�Ƃ��l������B

  type range = {min : int, max : int}

  type labeledRange = range anyLabeled

�������A���̕��@�͔ώG�ł���B


==========
4.6.3. ���[�J���t�H�[�}�b�g�^�O

��L�̖��ɑ΂��āASMLFormat�ł́A�����^���Ƀ}�b�`���鎯�ʎq���A���̕����^���ɑ΂��Ĉꎞ�I�ɗ^����ꂽ�^���Ƃ��Ĉ����A���̌^�ɑ΂���t�H�[�}�b�g�w����A��`�^���R�����g���̕ʂ̃t�H�[�}�b�g�^�O�Œ�`�ł���悤�ɂ��Ă���B

���̃t�H�[�}�b�g�^�O���A���[�J���Ɍ^��錾���A����ɑ΂���t�H�[�}�b�g���L�q����Ƃ����Ӗ��ŁA���[�J���t�H�[�}�b�g�^�O�ƌĂԁB
���[�J���t�H�[�}�b�g�^�O�ɑ΂��āA��`�^���ɑ΂���t�H�[�}�b�g�^�O���v���C�}���t�H�[�}�b�g�^�O�ƌĂԁB

���[�J���t�H�[�}�b�g�^�O�́A

  "@format:"ID "(" typepat ")" template ... template

�Ƃ����`���ŋL�q����B�i"@format:"��ID�̊Ԃɂ͋󔒂�����ł͂Ȃ�Ȃ��B�j

���[�J���t�H�[�}�b�g�^�O��p���āA�����قǂ�labeledRange�ɑ΂��āA���̂悤�Ƀt�H�[�}�b�g�R�����g���L�q�ł���B

  (*% *)
  type labeledRange =
       (*%
        * @format(range any) any(range)("Range:")
        * @format:range({min : min, max : max})
        *         "(min =" + min "," + "max =" + max ")"
        *)
       {min : int, max : int} anyLabeled

SMLFormat�́A�v���C�}���t�H�[�}�b�g�^�O�̃^�C�v�p�^�[��

  range any

�ƒ�`�^��

  {min : int, max : int} anyLabeled

�Ƃ̃p�^�[���}�b�`����A���ʎqrange���^{min : int, max : int}�����Ɣ��肷��B
�܂��A���̉��̃��[�J���t�H�[�}�b�g�^�O�́A���ʎqrange���A�^�C�v�p�^�[��

  {min : min, max : max}

�ƃ}�b�`����^�����w�����O�Ƃ��Ĉ������Ƃ��w������B
SMLFormat�́A�����𑍍����āA���[�J���Ɉȉ��̂悤�ɐ錾���ꂽ���̂Ƃ݂Ȃ��B

  type range = {min : int, max : int}

�����āA�v���C�}���t�H�[�}�b�g�^�O���̎��ʎqrange��range�^�������̂Ƃ��Ĉ����Arange�^�ɑ΂���t�H�[�}�b�^�����[�J���t�H�[�}�b�g�^�O���琶������B


SMLFormat�́A���̃t�H�[�}�b�g�R�����g������̃t�H�[�}�b�^�֐��𐶐�����B

  fun format_labeledRange x =
      let
        fun format_range x =
            case x of
              {min = min, max = max} =>
              <<
                "(min =" + format_int(min) "," +
                "max =" + format_int(max) ")"
              >>
      in
        case x of
          any => format_anyLabeled (format_range, "Range:") any
      end

���l�ɁA����tree�^�ɂ����[�J���t�H�[�}�b�g�^�O���g�p���ăt�H�[�}�b�g�R�����g���L�q���邱�Ƃ��ł���B

  (*% *)
  datatype tree =
                 (*% @format(num) num *)
                 Leaf of int
               | (*%
                  * @format(child children) "{" children(child)(", ") "}"
                  * @format:child({child, label}) label "=" child
                  *)
                 Node of {child : tree, label : string} list

���̃t�H�[�}�b�^�����������B

  fun format_tree x =
      case x of
          Leaf x => case x of num => << (format_int num) >>
        | Node x =>
          let
            fun format_child x =
                case x of
                    {child, label} =>
                    << (format_string label) "=" (format_tree child) >>
          in
            case x of
                children =>
                << "{" (format_list(format_child, ", ") children) "}" >>
          end


�l�X�g�����^�\���q�K�p�^���ɂ��Ă����l�Ƀ��[�J���t�H�[�}�b�g�^�O���g�p���邱�Ƃ��ł���B��قǂ�maybeLabeledInt���ȉ��̂悤�ɏ����������Ƃ��ł���B

  (*% *)
  type maybeLabeledInt =
                        (*%
                         * @format(maybeNum any) any(maybeNum)("INT")
                         * @format:maybeNum(num may) may(num)
                         *)
                        int maybe anyLabeled

���̃t�H�[�}�b�^�����������B

  fun format_maybeLabledInt x =
      case x of
         any =>
           let
             fun format_maybeNum x =
                 case x of may => format_maybe format_int may
           in
             << format_anyLabeled (format_maybeNum, "INT") any >>
           end


���ӁF�t�H�[�}�b�g�^�O�́A���ׂ鏇���Ɋւ��Ă��̐���ɂ�������Ȃ���΂Ȃ�Ȃ��B
�����`�^���ɑ΂���t�H�[�}�b�g�R�����g�ŁA�t�H�[�}�b�g�^�O��

  @format( typepat0 ) ...
  @format:ID1( typepat1 ) ...
       :
  @format:IDk( typepatk ) ...

�̏��ɕ���ł���Ƃ��AIDi (1 <= i <= k)�́Atypepat0 ... typepat(i-1)�̂����ꂩ�ɏo�����ĂȂ���΂Ȃ�Ȃ��B
�Ƃ��ɁA�v���C�}���t�H�[�}�b�g�^�O�����[�J���t�H�[�}�b�g�^�O����ɋL�q���邱�Ƃ��K�v�ł���B


====================
4.7. �J�X�^���t�H�[�}�b�^

�t�H�[�}�b�g�R�����g�́A���ꂼ���type/datatype�錾�ɂ��Ĉ�񂵂��L�q�ł��Ȃ��B�܂��Aint��string�ȂǃV�X�e���⃉�C�u�����Œ�`����Ă���^�ɑ΂��ăt�H�[�}�b�g�R�����g���L�q���邱�Ƃ��ł��Ȃ��B

SMLFormat�́A����^�ɑ΂��Ă��łɒ�`����Ă���t�H�[�}�b�^�ɑւ��ĕʂ̃t�H�[�}�b�^���g�p������A�V�X�e���⃉�C�u�����ȂǂŒ�`����Ă���^�ɑ΂���t�H�[�}�b�^���w�肷���i��p�ӂ��Ă���B������J�X�^���t�H�[�}�b�^�ƌĂԁB

���Ƃ��΁Aword�l��2�i�\�L�Ńt�H�[�}�b�g����t�H�[�}�b�^��MyFormatters�X�g���N�`���ɒ�`���ABasicFormatters.format_word�ɑւ��Ĉꕔ�ł�����g�p�������Ƃ���B

  fun myformat_binary x = << "0b" (Word.fmt StringCvt.BIN x) >>

����type/datatype�錾�̃t�H�[�}�b�g�R�����g���ŃJ�X�^���t�H�[�}�b�^���g�p����ɂ́A�錾�w�b�_�R�����g���ɂ��̃^�O��p���Ďw�肷��B

  @formatter(ID) qid

���̃^�O�́AID���[���I�Ȍ^���Ƃ��Đ錾���A���̌^�ɑ΂���t�H�[�}�b�^�Ƃ���qid���w���֐����g�p���邱�Ƃ��w������B
�Ȃ��Aqid�͂��̐����K���ɂ��������B

  qid ::= ID
       |  ID "." qid

�����āA��`�^���R�����g���ŁA���ʎq�ɑ΂��Ďg�p����J�X�^���t�H�[�}�b�^���w�肷��B�^�C�v�p�^�[�����Ŏw�肷����@�ƁA�e���v���[�g���Ŏw�肷����@�̓�ʂ�Ŏw��ł���B

��������B

  (*%
   * @formatter(binary) MyFormatters.myformat_binary
   *)
  type file =
             (*%
              * @format({name, flags : flags:binary})
              *           "{name=" name "," + "flags=" flags "}"
              *)
             {name : string, flags : word}

���邢�͂��̂悤�ɂ�������B

  (*%
   * @formatter(binary) MyFormatters.myformat_binary
   *)
  type file =
             (*%
              * @format({name, flags})
              *           "{name=" name "," + "flags=" flags:binary "}"
              *)
             {name : string, flags : word}

����file�^�ɑ΂��āA���̃t�H�[�}�b�^�����������B

  fun format_file x =
      case x of
        {name, flags} =>
          <<
            "{name=" (format_string name) "," +
            "flags=" (MyFormatters.myformat_binary flags) "}"
          >>


====================
4.8. �`���I��`

�l�X�g�����^�C�v�p�^�[���A���[�J���t�H�[�}�b�g�^�O�A�J�X�^���t�H�[�}�b�^���T�|�[�g���邽�߁A�`���I��`���g������B


==========
4.8.1. �p�^�[���}�b�`


�܂��A�^��t�ƃ^�C�v�p�^�[��tp����^��T�𓾂�K��

    t <=> tp ==> T

���`����B

(IDmatch)
    t <=> ID ==> {ID:t}

(TypedIDmatch)
    t <=> ID1 : ID2 ==> {ID1:ID2}

(TYCONAPPmatch)
    t1 <=> p1 ==> T1  ...  tj <=> pj ==> Tj
    ----------------------------------------
    (t1, ..., tj) t <=> (p1, ..., pj) ID
          ==> T1+...+Tj+{ID:(t1, ..., tj) t}

(TypedTYCONAPPmatch)
    t1 <=> p1 ==> T1  ...  tj <=> pj ==> Tj
    ----------------------------------------
    (t1, ..., tj) t <=> (p1, ..., pj) ID1 : ID2
          ==> T1+...+Tj+{ID1:(t1, ..., tj) ID2}

(TUPLEmatch)
    t1 <=> p1 ==> T1  ...  tj <=> pj ==> Tj
    -------------------------------------------
    (t1, ..., tj) <=> (p1,... pj) ==> T1+...+Tj


==========
4.8.2. SML�p�^�[���̐���

���ɁA�^�C�v�p�^�[��typat����ASML�̃p�^�[��expat�𓾂�K��

    typat --> expat

�������B

(IDpat)
    ID --> ID

(TypedIDpat)
    ID1 : ID2 --> ID1

(TYCONAPPpat)
    (tp, ..., tp) ID --> ID

(TypedTYCONAPPpat)
    (tp, ..., tp) ID1 : ID2 --> ID1

(TUPLEpat)
    tp1 --> pat1    ...    tpj --> patj
    ----------------------------------------
    (tp1 * ... * tpj) -->  (pat1, ..., patj)


==========
4.8.3. �t�H�[�}�b�g�e���v���[�g�ϊ�

INST1���C������ƂƂ��ɁATypedINST1�ATypedINST2��ǉ�����B

(INST1)
    T(ID) = (t1,...,tj) t
    F(t) = f
    F,T,P | inst1 ARG==> f1
           :
    F,T,P | instj ARG==> fj
    F,T,P | temp1 ==> exp1
           :
    F,T,P | tempk ==> expk
    -----------------------------------------------
    F,| ID(inst1,...,instj)(temp1,..., tempk)
    T,|       ==>
    P |        f(f1, ..., fj, exp1, ... , expk)(ID)

(TypedINST1)
    F(ID2) = f
    F,T,P | inst1 ARG==> f1
           :
    F,T,P | instj ARG==> fj
    F,T,P | temp1 ==> exp1
           :
    F,T,P | tempk ==> expk
    -----------------------------------------------
    F,| ID1:ID2(inst1,...,instj)(temp1,..., tempk)
    T,|      ==>
    P |       f(f1, ..., fj, exp1, ... , expk)(ID1)

(TypedINST2)
    F(ID2) = f
    --------------------------
    F,T,P | ID1:ID2  ==>  f(ID1)


INST1��TypedINST1�́A�e���v���[�g�C���X�^���V�G�[�V��������t�H�[�}�b�^�֐����ւ̕ϊ�

    F,T,P | inst ARG==> formatter

��p����B���̕ϊ��͂��̎l�K�����琬��B

(INST1-ARG)
    T(ID) = (t1,...,tj) t
    F(t) = f
    F,T,P | inst1 ARG==> f1
           :
    F,T,P | instj ARG==> fj
    F,T,P | temp1 ==> exp1
           :
    F,T,P | tempk ==> expk
    --------------------------------------------
    F,| ID(inst1,...,instj)(temp1,..., tempk)
    T,|        ARG==>
    P |         f(f1, ..., fj, exp1, ... , expk)

(TypedINST1-ARG)
    F(ID2) = f
    F,T,P | inst1 ARG==> f1
           :
    F,T,P | instj ARG==> fj
    F,T,P | temp1 ==> exp1
           :
    F,T,P | tempk ==> expk
    -----------------------------------------------
    F,| ID1:ID2(inst1,...,instj)(temp1,..., tempk)
    T,|         ARG==>
    P |            f(f1, ..., fj, exp1, ... , expk)

(INST2-ARG)
    T(ID) = t    F(t) = f
    ---------------------
    F,T,P | ID  ARG==>  f

(TypedINST2-ARG)
    F(ID2) = f
    -------------------------------
    F,T,P | ID1:ID2  ARG==>  f(ID1)


==========
4.8.4. �t�H�[�}�b�^�{�̂̐���


�ȏ��p����ƁA�O�Ɏ�����(TUPLEtype)��(TYCONAPPtype)�͂��̋K���ɓ����ł���B

(TYPEtype)
    t <=> typepat ==> T
    T(ID1) <=> typepat1 ==> T1
    (T+T1)(ID2) <=> typepat2 ==> T2
         :
    (T+T1+...+Tj-1)(IDj) <=> typepatj ==> Tj
    Fj = F                            (* register formatters for alias type *)
    Fj-1 = Fj+{IDj:fj}
         :
    F1 = F2+{ID2:f2}
    F0 = F1+{ID1:f1}
    T' = T+{ID1:ID1,...,IDj:IDj}    (* overwrites entries for alias type *)
    T1' = T1+{ID2:ID2,...,IDj:IDj}
         :
    Tj' = Tj
    F0,T',P | templates ==> exp       (* generate exps for templates *)
    F1,T1',P | templates1 ==> exp1
         :
    Fj,Tj',P | templatesj ==> expj
    typepat --> pat
    typepat1 --> pat1
         :
    typepatj --> patj
    -----------------------------------------------------
    F, | @format(typepat) templates
    P  | @format:ID1(typepat1) templates1
       |       :
       | @format:IDj(typepatj) templatesj
       | x, t
       |
       |     ==>
       |
       |       case x of pat => let fun fj patj = expj
       |                               :
       |                            fun f1 pat1 = exp1
       |                        in exp end


==========
4.8.5. �t�H�[�}�b�^�̐���

TYPEdec��DATATYPEdec���A�錾�w�b�_�R�����g�ł̃J�X�^���t�H�[�}�b�^�^�O�ɑΉ����Ċg������B

TYPEdec,DATATYPEdec�̂�������A��`�^���ɑ΂���t�H�[�}�b�^�R�[�h�𐶐�����ۂɎg�p����t�H�[�}�b�^���ɁA�J�X�^���t�H�[�}�b�^�^�O�Ŏw�肳�ꂽ�t�H�[�}�b�^��������B

(TYPEdec)
    x,f1,...,fj are fresh names
    F' = F+{t:format_t}
    F'' = F'+{'a1:f1, ..., 'aj:fj}+{ID1:qid1, ..., IDn:qidn}
    P = {b1,...,bk}
    F'',P | formattag,x,t  ==>  e
    -------------------------------------------------------
    F | (*%
      |    @formatparams (b1,...,bk)
      |    @formatter(ID1) qid1
      |          :
      |    @formatter(IDn) qidn
      |  *)
      | type ('a1,...,'aj) t =
      |         (*% formattag *) t
      |    ==>
      |            fun format_t(f1,...,fj,b1,...,bk) x = e,
      |            F'

(DATATYPEdec)
    x,x1,...,xj,f1,...,fj are fresh names
    F' = F+{t:format_t}
    F'' = F'+{'a1:f1, ..., 'aj:fj}+{ID1:qid1, ..., IDn:qidn}
    P = {b1, ..., bk}
    F'',P | formattag1,x1,t1  ==> e1
             :
    F'',P | formattagj,xj,tj  ==> ej
    -----------------------------------------------------
    F | (*%
      |    @formatparams (b1,...,bk)
      |    @formatter(ID1) qid1
      |          :
      |    @formatter(IDn) qidn
      |  *)
      | datatype ('a1,...,'aj) t =
      |           (*% formattag1 *)  D1 of t1
      |         | ...
      |         | (*% formattagj *)  Dj of tj 
      |    ==>
      |      fun format_t(f1,...,fj,b1,...,bk) x =
      |          case x of D1 x1 => e1 | ... | Dj xj => ej,
      |      F'


====================
4.9. �f�t�H���g�t�H�[�}�b�g�^�O����

�^�錾�w�b�_�R�����g���w�肳��Ă���ɂ�������炸�A�����`�^���ɒ�`�^���R�����g���w�肳��Ă��Ȃ��ꍇ�ASMLFormat�͂��̒�`�^���ɑ΂��ăt�H�[�}�b�g�^�O��������������B������f�t�H���g�t�H�[�}�b�g�^�O�ƌĂԁB�����āA���̃^�O�����ƂɁA�v���O���}�������I�Ƀ^�O���w�肵���ꍇ�Ɠ��l�ɁA�t�H�[�}�b�^�R�[�h�𐶐�����B

���Ƃ��΁A���̂悤�Ɍ^�\���qmaybe����`����Ă���ꍇ

  (*% *)
  datatype 'a maybe =
                     Something of 'a
                   | (*% @format "none" *)
                     Nothing

SMLFormat�́A�l�\���qSomething�ɑ΂��Ă��̂悤�ȃf�t�H���g�t�H�[�}�b�g�^�O�𐶐����������Ńt�H�[�}�b�^�R�[�h�𐶐�����B

  (*% *)
  datatype 'a maybe =
                     (*% @format(x1) {"Something" + {x1}} *)
                     Something of 'a
                   | (*% @format "none" *)
                     Nothing

�f�t�H���g�t�H�[�}�b�g�^�O�ɂ���Đ��������t�H�[�}�b�^�́A�t�H�[�}�b�g�Ώۂ̒l���ASML�̃\�[�X�R�[�h��ŋL�q����ꍇ�Ɠ��l�̌`���Ƀt�H�[�}�b�g����B
���Ƃ��� {name : string, age : int} �^�̒l {name = "YAMADA", age = 20} ��
  {name = "YAMADA", age = 20}
�Əo�͂��Aint list�^�̒l [1, 3, 5] ��
  [1, 3, 5]
�Əo�͂���B

�f�t�H���g�t�H�[�}�b�g�^�O�̐����K�����ȉ��Ɏ����B
���̋K���́A�^��t����A�ЂƂ̃v���C�}���t�H�[�}�b�g�^�O��j�̃��[�J���t�H�[�}�b�g�^�O�̃��X�g�𐶐����邱�Ƃ�

  t => (typat, temps), {(id1, typat1, temps1), ..., (idj, typatj, tempsj)}

�̂悤�ɕ\�L����B
�v���C�}���^�O�̓^�C�v�p�^�[��typat�ƃe���v���[�g��temps����\�������B
���[�J���t�H�[�}�b�g�^�O�́A���ʎq�A�^�C�v�p�^�[���A�e���v���[�g�񂩂�\�������B

(ATOMdeftags)
    x is fresh name
    ---------------
    ID
    =>
    (x, x), {}

(TUPLEdeftags)
    x1,...,xj are fresh names
    t1 => ((typat1, temp1), L1)
         :
    tj => ((typatj, tempj), Lj)
    ---------------------------------------------------------
    t1 * ... * tj
    =>
    ((x1, ..., xj), "(" x1 "," ... "," xj ")"),
    {(x1, typat1, temp1), ..., (xj, typatj, tempj)}+L1+...+Lj

(RECORDdeftags)
    x1,...,xj are fresh names
    t1 => ((typat1, temp1), L1)
         :
    tj => ((typatj, tempj), Lj)
    -----------------------------------------------------------------------
    {ID1:t1, ..., IDj:tj}
    =>
    ({ID1=x1, ..., IDj=xj}, "{" "ID1" "=" x1 "," ... "," "IDj" "=" xj "}"),
    {(x1, typat1, temp1), ..., (xj, typatj, tempj)}+L1+...+Lj

(TYCONAPPdeftags)
    x1,...,xj are fresh names
    t1 => ((typat1, temp1), L1)
         :
    tj => ((typatj, tempj), Lj)
    ---------------------------------------------------------
    (t1, ..., tj) ID
    =>
    ((x1, ..., xj) x, x(x1, ..., xj)),
    {(x1, typat1, temp1), ..., (xj, typatj, tempj)}+L1+...+Lj

t��List.list��K�p����^�\���q�K�p�^���ł���ꍇ�́A�ǉ�������list�p�̃t�H�[�}�b�^(BasicFormatters.format_list)�ɓn���K�v������̂ŋK����ʂɐ݂���B

(listAPPdeftags)
    x1,...,xj are fresh names
    t1 => ((typat1, temp1), L1)
         :
    tj => ((typatj, tempj), Lj)
    ---------------------------------------------------------
    (t1, ..., tj) list
    =>
    ((x1, ..., xj) x, "[" x(x1, ..., xj)(",") "]"),
    {(x1, typat1, temp1), ..., (xj, typatj, tempj)}+L1+...+Lj

t��Array.array��Vector.vector��K�p����^�\���q�K�p�^���ł���ꍇ�����l�ł���B

t���֐��^�ł���ꍇ�́A�l���p�^�[���}�b�`�ɂ���Ă���ȏ��͂��邱�Ƃ��ł��Ȃ��̂ŁA�Œ�̕�����萔���o�͂���B

(funAPPdeftags)
    x is a fresh name.
    --------------
    t1 -> t2
    =>
    (x, "<<fn>>"),
    {}

type�錾���̒�`�^���ɂ��ẮA�ȏ�̋K���œ�����v���C�}���t�H�[�}�b�g�^�O�����̂܂܎g�p����B
datatype�錾���̒�`�^���ɂ��ẮA�ȏ�̋K���œ�����v���C�}���t�H�[�}�b�g�^�O�̃e���v���[�g��̐擪�ɁA�l�\���q�̖��O��}�����������Ŏg�p����B

====================
4.10. �^�錾

==========
4.10.1. ���ݍċA����datatype

�ЂƂ�datatype�錾����and�ɂ��A������Đ錾���ꂽ�����̃f�[�^�^�ɑ΂��ẮA���ݎQ�Ƃ���t�H�[�}�b�^�R�[�h�����������B

==========
4.10.2. withtype

withtype��p�����^�錾�ɑ΂��Ă��t�H�[�}�b�g�R�����g���L�q�ł���B


  (*% *)
  datatype tree =
                 (*% @format(num) num *)
                 Leaf of int
               | (*% @format(child children) "{" children(child)(", ") "}" *)
                 Node of node list

  withtype node =
                 (*% @format({child, label}) label "=" child *)
                 {child : tree, label : string}

���̃t�H�[�}�b�^�����������B

  fun format_tree x =
      case x of
          Leaf x => case x of num => << (format_int num) >>
        | Node x =>
          case x of
              children =>
              << "{" (format_list(format_node, ", ") children) "}" >>
  and format_node x =
      case x of
          {child, label} =>
              << (format_string label) "=" (format_tree child) >>

==========
4.10.3. abstype

abstype�ɂ������Ă��t�H�[�}�b�g�R�����g���L�q�ł���B

    (*% *)
    abstype set =
            (*% @format(element elements) "{" elements(element)(", ") "}" *)
            SET of element list
    withtype element =
             (*% @format(value) value *)
             string
    with
      fun create () = ...
      fun addTo (v, set) = ...
      fun isMemberOf (v, set) = ...
    end

����܂łɂ݂��^�錾�ł̓t�H�[�}�b�^�R�[�h�͐錾�̒���ɐ�������Ă������Aabstype�ɑ΂��Ă̓t�H�[�}�b�^�R�[�h�͂��̂悤�ɐ錾�����ɐ��������B

    (*% *)
    abstype set =
            (*% @format(element elements) "{" elements(element)(", ") "}" *)
            SET of element list
    withtype element =
             (*% @format(value) value *)
             string
    with
      fun format_set x = ...
      and format_element x = ...
      fun create () = ...
      fun addTo (v, set) = ...
      fun isMemberOf (v, set) = ...
    end

====================
4.10.4 datatype replication

  datatype s = datatype M.t

�̌`���Œ�`�����f�[�^�^�ɂ��ẮA���̂悤�Ƀt�H�[�}�b�^���w�肷��B

    (*%
     * @formatter(Absyn.region) Absyn.format_region
     *)
    datatype region = datatype Absyn.region


====================
4.12. exception�錾

��O�\���q�ɑ΂��Ă��t�H�[�}�b�g�R�����g���L�q�ł���B
  
  (*% *)
  exception
  (*%
   * @format({fileName, line : leftLine, col : leftCol} *
   *         {line : rightLine, col : rightCol, ...} *
   *         message)
   * fileName ":" leftLine "." leftCol "-" rightLine "." rightCol ":" message
   *)
  ParseError of
  {fileName:string, line:int, col:int} *
  {fileName:string, line:int, col:int} *
  string

��O�\���q���g���\�ł��邽�߁A��O�\���q�ɑ΂��Đ��������t�H�[�}�b�^�́A�ȉ��̂悤�ɁAdatatype�錾��type�錾�ɑ΂��Đ��������t�H�[�}�b�^�Ƃ͈قȂ�B

  local
    fun format x =
        case x of
          ParseError
          (
            {fileName, line = leftLine, col = leftCol},
            {line = rightLine, col = rightCol, ...},
            message
          ) =>
           <<
             (format_string fileName) ":"
             (format_int leftLine) "." (format_int leftCol) "-"
             (format_int rightLine) "." (format_int rightCol) ":"
             message
           >>
        | _ => (!SMLFormat.BasicFormatters.format_exn_Ref) x
    val _ = SMLFormat.BasicFormatters.format_exn_Ref := format
  in end

format_exn_Ref�����format_exn�́ABasicFormatters�X�g���N�`���ɂ��̂悤�ɒ�`����Ă���B

  val format_exn_Ref =
      ref
      (fn exn => 
          let val text = General.exnMessage exn
          in [FE.Term (size text, text)] end)

  fun format_exn exn = !format_exn_Ref exn

����: ���݂�SMLFormat�̎����́A��O�錾�ɑ΂���t�H�[�}�b�^�����ɂ��āA�ȉ��̓�_�Ɋւ��Đ�����݂��Ă���B
1, ��q��"prefix"�^�O���O�錾�ɑ΂��Ďw��ł��Ȃ��B
2, ���̌`���̗�O��`�ɑ΂��ăt�H�[�}�b�g�R�����g���w�肷�邱�Ƃ͂ł��Ȃ��B
     exception E1 = E2

====================
4.12. prefix�^�O

prefix�^�O��p����ƁA�ЂƂ�type/datatype�錾�ɑ΂��ĕ����̃t�H�[�}�b�^�������������邱�Ƃ��ł���B
prefix�^�O�͕��������p�����[�^�Ɏ��B���̃p�����[�^�͐��������t�H�[�}�b�^�̖��O�̃v���t�B�N�X�Ƃ��Ďg�p�����B

  (*%
   * @prefix summary
   *)
  (*%
   * @prefix detail
   *)
  type address =
                (*%
                 * @prefix summary
                 * @format({zip, state, city}) state
                 *)
                (*%
                 * @prefix detail
                 * @format({zip, state, city})
                 *   "zip=" zip ",state=" state ",city=" city
                 *)
                {zip : string, state : string, city : string}

����address�ɑ΂��āA���̓�̃t�H�[�}�b�^�����������B

  fun summaryaddress x =
      case x of
        {zip, state, city} => << format_string state >>

  fun detailaddress x =
      case x of
        {zip, state, city} =>
          <<
            "zip=" (format_string zip)
            ",state=" (format_string state)
            ",city=" (format_string city)
          >>

prefix�^�O�͖��O��Ԃ��w�肷��Ӗ������B
����t�H�[�}�b�^����́A�����v���t�B�N�X���w�肳�ꂽ�t�H�[�}�b�g�R�����g���琶�����ꂽ�t�H�[�}�b�^�݂̂��Ăяo�����Ƃ��ł���B
�i��O�Ƃ��āAformat_string��format_int�Ȃ�BasicFormatters�Ő錾����Ă���t�H�[�}�b�^�͂�����̃t�H�[�}�b�^������Ăяo�����Ƃ��ł���B�j

  (*%
   * @prefix detail
   *)
  type customer = 
                  (*%
                   * @prefix detail
                   * @prefix({name, address, tel})
                   *      "name=" name ",address=" address ",tel=" tel
                   *)
                  {name : string, address : address, tel : string}

�ȉ��̂悤�ɁA����customer�ɑ΂��Đ��������t�H�[�}�b�^�́Aaddress�ɑ΂��Đ������ꂽ�t�H�[�}�b�^�̂����Aprefix��detail�Ƃ���t�H�[�}�b�^������ŌĂяo���B

  fun detailcustomer x =
      case x of
        {name, address, tel} =>
          <<
            "name=" (format_string name)
            ",address=" (detailaddress address)
            ",tel=" (format_string tel)
          >>

�قȂ�prefix���w�肳�ꂽ�t�H�[�}�b�^���Ăяo���ɂ́Aformatter�^�O���g�p����B

prefix�^�O���w�肳��Ă��Ȃ��ꍇ�́A"format_"���v���t�B�N�X�Ɏw�肳�ꂽ���̂Ƃ݂Ȃ��B

datatype�ɂ��Ă����l��prefix�^�O��p���ĕ����̃t�H�[�}�b�^�𐶐��ł���B

  (*%
   * @prefix formatPlain
   *)
  (*%
   * @prefix formatHTML
   *)
  datatype block = 
                  (*%
                   * @prefix formatPlain
                   * @format(text) text
                   *)
                  (*%
                   * @prefix formatHTML
                   * @format(text) text
                   *)
                  Text of string
                |
                  (*%
                   * @prefix formatPlain
                   * @format(block) block
                   *)
                  (*%
                   * @prefix formatHTML
                   * @format(block) "<B>" block "</B>"
                   *)
                   Bold of block

���̃t�H�[�}�b�^�����������B

  fun formatPlainblock x =
      case x of
        Text text => << format_string text >>
      | Bold block => << formatPlainblock block >>

  fun formatHTMLblock x =
      case x of
        Text text => << format_string text >>
      | Bold block => << "<B>" formatHTMLblock block "</B>" >>


========================================
